package com.GreenIce.android.util;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.util.Log;

public class DistanceSensor implements SensorEventListener {
	static DistanceSensor inst;
    private Sensor distSensor;

	public static DistanceSensor getInstance(){
		if (inst == null)
			inst = new DistanceSensor();
		return inst;
	}
	public static void setHandler(Handler handler){
		getInstance().handler = handler;
	}
	public static void enable(){
		getInstance().enable = true;
	}
	public static void disable(){
		getInstance().enable = false;
	}
	private Handler handler;
	private boolean enable;
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		Log.i("DistanceSensor","onSensorChanged:"+event.values[0]);
		if (enable){
			handler.sendEmptyMessage((int) event.values[0]);
		}
	}
	public void register(SensorManager mSensorManager) {
		if (distSensor == null)
			distSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        mSensorManager.registerListener(this, distSensor, SensorManager.SENSOR_DELAY_GAME);		
	}
	public void unregister(SensorManager mSensorManager) {
		mSensorManager.unregisterListener(this);
	}
	
}
